
//
// 110305 - AH - (Fantoft) Created.
// 110306 - AH - (Fantoft Club) Implemented populate.
// 110311 - AH - Implemented scanAndPaint(Set<String>, List<AttributeObject>)
//               as done by ordinary Wheel.
// 110617 - AH - (Bybanen *) populate is now final in base class, renamed
//               populate so implements new doPopulate method.
// 120402 - AH - (Garage) Stamp logic.
//

package org.soekrat.engine.sparewheel.ontology;

import static org.soekrat.engine.swnetwork.SpareWheelNetworkCluster.retrieveCluster;

import java.util.List;

import org.soekrat.AttributeObject;
import org.soekrat.engine.network.Network;
import org.soekrat.engine.swnetwork.SizeSpareWheelClusterComparator;
import org.soekrat.engine.swnetwork.SpareWheelClusterComparator;
import org.soekrat.engine.swnetwork.SpareWheelNetwork;
import org.soekrat.engine.swnetwork.SpareWheelNetworkCluster;
import org.soekrat.engine.swnetwork.SpareWheelNetworkNode;
import org.soekrat.engine.visitor.Line;
import org.soekrat.engine.visitor.Lines;
import org.soekrat.engine.visitor.TokenizedLine;
import org.soekrat.engine.wheel.retrieval.RetrievalEvent;

/**
 * <p>
 *   {@link SpareWheelOntology} for a
 *   {@link Network}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class NetworkSpareWheelOntology extends SpareWheelOntology 
{
  private final SpareWheelNetwork m_network;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public NetworkSpareWheelOntology(){ this(null); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param network Ontolology {@code this} access, if {@code null} creates
   *                empty.
   */
  public NetworkSpareWheelOntology(SpareWheelNetwork network)
  {
    m_network=(network==null) ? new SpareWheelNetwork() : network;
    stamp();
  }
  
  @Override
  protected void doPopulate(Lines lines) 
  {
    if (lines==null)
    {
      throw new NullPointerException("lines");
    }
    
    for (Line line : lines)
    {
      TokenizedLine input=line.getInput();
      m_network.populate(input);
    }
  }
  
  @Override
  protected void stamp() 
  {
    if (m_network!=null)
    {
      String stamp=getStamp();
      for (SpareWheelNetworkNode node : m_network) 
        node.setAttribute("stamp", stamp);
    }
  }

  /**
   * <p>
   *   Gets ontology {@code this} access.
   * </p>
   * @return {@link Network}.
   */
  public SpareWheelNetwork getNetwork(){ return m_network; }
  
  @Override
  public TokenizedLine getTokens(List<AttributeObject> paint) 
  {
    TokenizedLine retVal=new TokenizedLine();
    
    for (AttributeObject curr : paint)
    {
      if (curr instanceof SpareWheelNetworkNode)
      {
        retVal.add(curr.getName());
      }
    }
    
    return retVal;
  }
  
  private SpareWheelClusterComparator m_comp=
    SizeSpareWheelClusterComparator.INSTANCE;
  
  public RetrievalEvent scanAndPaint(TokenizedLine tokens, 
    List<AttributeObject> paint) 
  {
    if (tokens==null)
    {
      throw new NullPointerException("tokens");
    }
    if (paint==null)
    {
      throw new NullPointerException("paint");
    }
    
    SpareWheelNetworkCluster retrieved=
      retrieveCluster(m_network, tokens, m_comp);
    
    if (retrieved!=null)
    {
      SpareWheelNetworkNode[] nodes=retrieved.getAllNodes();
      for (SpareWheelNetworkNode node : nodes) paint.add(node);
    }
    
    return null;
  }
  
  @Override
  public String toString(){ return m_network.toString(); }
  
}
